//
//  MineVC.swift
//  LeTu
//
//  Created by gy on 2023/11/24.
//

import UIKit

class MineVC: BaseViewController {

    private var viewModel = MineVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        viewModel.updateItems()
        mineTableView.reloadData()
        guard kUserManager.isLogin else { return }
        getData()
    }
    func getData() {
        
        kUserManager.getUserInfomation { [weak self] dialog in
            guard let strongSelf = self else { return }
            DispatchQueue.main.async {
                strongSelf.mineTableView.reloadData()
            }
        }
        
        viewModel.getRedemptionSetting { [weak self] dialog in
            guard let strongSelf = self else { return }
            DispatchQueue.main.async {
                strongSelf.mineTableView.reloadData()
            }
        }
        
    }
    
    // MARK: - configUI
    func configUI() {
            
        view.addSubview(mineTableView)
        mineTableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        mineTableView.delegate = self
        mineTableView.dataSource = self
        mineTableView.register(cellClass: MineArrowTableViewCell.self)
        mineTableView.register(cellClass: MineUserInfomationTableViewCell.self)
        
    }
    private lazy var mineTableView: UITableView = {
        let mineTableView = UITableView()
        mineTableView.tableFooterView = UIView()
        mineTableView.backgroundColor = .app_viewBackgroundColor
        mineTableView.showsVerticalScrollIndicator = false
        mineTableView.isScrollEnabled = false
        return mineTableView
    }()

}

extension MineVC: TransferEventReceivable {

    enum TransferEvent: TransferEventType {
        case loginAction
        case setAction
        case vipCardAction
    }
    
    func receive(event: TransferEventType) {
        
        switch event {
        case MineVC.TransferEvent.loginAction:
            showDetailViewController(LoginVC(viewType: .authCode), sender: nil)
        case MineVC.TransferEvent.setAction:
            guard kUserManager.isLogin else { return kUserManager.toLogin() }
            show(SetVC(), sender: nil)
        case MineVC.TransferEvent.vipCardAction:
            guard kUserManager.isLogin else { return kUserManager.toLogin() }
            show(VipCenterVC(), sender: nil)
        default:
            break
        }
        
    }
    
}

extension MineVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return section == 0 ? 10:0
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footer = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: tableView.sectionFooterHeight))
        footer.backgroundColor = .app_viewBackgroundColor
        return footer
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return indexPath.section == 0 ? MineUserInfomationTableViewCell.getCellHeight(isVip: kUserManager.userModel.memberName.count > 0) : 68
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? 1 : viewModel.items.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(MineUserInfomationTableViewCell.self, indexPath: indexPath)
            cell.setUserInfo()
            return cell
        }
        let cell = tableView.dequeueReusableCell(MineArrowTableViewCell.self, indexPath: indexPath)
        cell.setItem(viewModel.items[indexPath.row])
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        guard indexPath.section == 1 else { return }
        guard kUserManager.isLogin else { return kUserManager.toLogin() }
        let item = viewModel.items[indexPath.row]
        if item.title == "填写好友邀请码".localized {
            show(SetInviteCodeVC(), sender: nil)
        }else if item.title == "激活卡".localized {
            show(ChangeNicknameVC(viewType: .inviteCode), sender: nil)
        } else if item.title == "购买记录".localized {
            show(PurchaseHistoryVC(), sender: nil)
        } else if item.title == "我的收藏".localized {
            show(CollectVC(), sender: nil)
        } else if item.title == "优惠券".localized {
            show(MineCouponsVC(), sender: nil)
        } else if item.title == "意见反馈".localized {
            show(FeedbackVC(), sender: nil)
        }
    }
    
}

